/**
 * 
 */
package org.cuatrovientos.erp.dao;

import java.util.List;

import org.cuatrovientos.erp.model.Employee;
import org.cuatrovientos.erp.model.Role;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author pablo_guerrero
 * This class implements EmployeeDAOInterface
 *
 */
public class EmployeeDAO implements EmployeeDAOInterface{
	
	@Autowired
	 private SessionFactory sessionFactory;
	
	/**
	 * default Constructor
	 */
	public EmployeeDAO(){
		
	}
	
	/**
	 * returns Hibernate session
	 * @return the session
	 */
	 private Session getSession(){
		 return sessionFactory.getCurrentSession();
	 }
	 
	/**
	 * @param int id
	 * @return Employee employee
	 * This method returns a employee searched by it's id
	 */
	@Override
	@Transactional(readOnly = true)
	public Employee selectById(int id) {
		Employee employee = (Employee) getSession().get(Employee.class, id);
	    return employee;
	}

	/**
	 * @return All the employees we have in the database
	 */
	@Override
	@Transactional(readOnly = true)
	public List<Employee> selectAll() {
		List<Employee> employees = getSession().createQuery("from Employee").list();
	    return employees;
	}

	/**
	 * @return The number of employees we have in the database
	 */
	@Override
	@Transactional(readOnly = true)
	public int selectAllCount() {
		List<Employee> employees = getSession().createQuery("from Employee").list();
	    return employees.size();
	}

	/**
	 * 
	 */
	@Override
	@Transactional
	public void saveOrUpdate(Employee employee) {
		getSession().saveOrUpdate(employee);
	}
	
	
	/**
	 * @param Employee employee
	 * This method insert a employee in the database passed by parameter
	 */
	@Override
	@Transactional
	public void insert(Employee employee) {
		Integer id = (Integer) getSession().save(employee);
		employee.setId(id);
	}

	/**
	 * @param Employee employee
	 * This method updates a employee passed by parameter
	 */
	@Override
	@Transactional
	public void update(Employee employee) {
		getSession().merge(employee);
	}

	/**
	 * @param Employee employee
	 * This method deletes a employee passed by parameter
	 */
	@Override
	@Transactional
	public void delete(Employee employee) {
		getSession().delete(employee);
		
	}

	/**
	 * @param int id
	 * This method deletes the employee selected by id
	 */
	@Override
	@Transactional(readOnly=true)
	public void deleteById(int id) {
		Employee employee = (Employee) getSession().load(Employee.class,id);
		getSession().delete(employee);
		
	}

	/**
	 * @param  String name
	 * This method searches all the employees which has the same name we are looking for.
	 */
	@Override
	@Transactional(readOnly=true)
	public List<Employee> search(String name) {
		Query query = getSession().createQuery("from Employee e where e.name like :name");
        query.setParameter("name", "%"+name+"%");
        List<Employee> employees = query.list();
		
        return employees;
	}


}
